import { DatabaseHelper } from './DatabaseHelper.js';

let myHandler = async function (event, context, callback, logger) {
  // logger.info(event);
  logger.info(JSON.stringify(event));
  let username: string = event.body ? JSON.parse(event.body).username : event.username;
  let password: string = event.body ? JSON.parse(event.body).password : event.password;
  let msg;
  try {
    const databaseHelper = new DatabaseHelper(context, logger);
    const user = await databaseHelper.queryUser(username, password);
    if (user) {
      if (user.password === password) {
        callback({
          code: 0,
          message: '登录成功',
          data: user,
        });
      } else {
        callback({
          code: 1,
          message: '密码错误，请输入正确密码',
          data: null,
        });
      }
    } else {
      callback({
        code: 2,
        message: '用户不存在',
        data: null,
      });
    }
  } catch (err) {
    logger.error("func error:" + err.message + " stack:" + err.stack);
    callback({
      code: 3,
      message: '登录失败',
      data: err,
    });
  }
};

export { myHandler };